﻿using Command.CommunityToolkit.Attributes;
using Command.CommunityToolkit.Helpers;

namespace Command.CommunityToolkit.Services;
/// <summary>
/// 质量控制流程定义
/// </summary>
public abstract class QualityControlBase : IQualityControl
{
    public const string LicenseDirectory = "LicenseDirectory";

    public abstract IEnumerable<string> UnZipLicenses(string zipPath);
    public abstract bool CopyLicense(string licenseResponseText);
    public abstract bool VerifyLicense();
    [Log("质量控制")]
    public bool Execute(string? zipPath = "")
    {
        if (!File.Exists(zipPath)) throw new ArgumentNullException($"没有找到证书压缩文件{zipPath}");
        var serviceProvider = ServiceProviderHelper.GetInstance().ServiceProvider;
        if (serviceProvider == null) throw new Exception("ServiceProviderHelper初始化异常");
        var licenses = UnZipLicenses(zipPath);
        var ok = false;
        foreach (var l in licenses)
        {
            try
            {
                CopyLicense(l);
                ok = ok || VerifyLicense();
                if (ok) break;
            }
            catch (Exception)
            {
                ok = false;
            }
        }
        return ok;

    }
}